<?php
    include_once('../../src/classes/class.mssql.php');
    
    $comtec_id_machine = $_GET['id'];
    $comtec_id_mainteiner = $_GET['mainteiner'];
   
    $db = new MSSQL();
    
    $texto = "SELECT comtec_id_owner,
		     comtec_id_configman,
		     tag_id_machine,
		     cod_mainteiner,
		     cod_owner,
                     brand,
		     model,            
                     year,
		     notes,
                     DATEPART(day,date_next_maintenance) as maintenance_day,                     
                     DATEPART(month,date_next_maintenance) as maintenance_month,
                     DATEPART(yyyy,date_next_maintenance) as maintenance_year,
                     comtec_id_responsible
		     FROM machines
                     WHERE comtec_id_machine=".$comtec_id_machine;
    
    $result = $db->fetch_array($db->query($texto));  
    $comtec_id_responsible = $result['comtec_id_responsible'];
    $comtec_id_owner=$result['comtec_id_owner'];
    $comtec_id_configman = $result['comtec_id_configman'];
    
    $tag_id_machine = $result['tag_id_machine'];
    $cod_mainteiner = $result['cod_mainteiner'];
    $cod_owner = $result['cod_owner'];
    $brand  = $result['brand'];  
    
    $model = $result['model'];   
    
    $year = $result['year'];
    $notes = $result['notes'];
    
    $maintenance_day  =  $result['maintenance_day'];
    $maintenance_month = $result['maintenance_month'];
    $maintenance_year  = $result['maintenance_year'];
    
     function extra($x){
     if ($x <= 9){
      $x = "0".$x;   
     }   
     return $x;   
    }
    
?>
<fieldset class="curved">
	  <legend>Editar</legend>
			<table style="margin: 10px auto; border-spacing: 10px;">
			    <tr>
				<td width="200">Tag Id.</td>
				<td><input type="text" size="40" maxlength="10" id="tag_id_machine" value="<?=$tag_id_machine;?>" /></td>
				<td width="200">Marca</td>
				<td><input type="text" size="40" maxlength="20" id="brand" value="<?=$brand;?>" /></td>
			    </tr>
			    <tr>
				<td width="200">Cod. Mantenedor</td>
				<td><input type="text" size="40" maxlength="10" id="cod_mainteiner" value="<?=$cod_mainteiner;?>" /></td>
				<td width="200">Modelo</td>
				<td><input type="text" size="40" maxlength="20" id="model" value="<?=$model;?>" /></td>
			    </tr>
			    <tr>
			        <td width="200">A&ntilde;o</td>
				<td><input type="text" size="40" maxlength="5" id="year" size="4" value="<?=$year;?>" /></td>
                                <td width="200">Encargado</td>
                                <td>
                                    <select id="comtec_id_responsible" style="width:257px;">
                                    <option value="0" selected>Selecci&oacute;n...</option>    
                                     <?
                                        $responsible = $db->query("SELECT comtec_id_responsible, name, lastname1, lastname2, status_reg FROM responsible WHERE comtec_id_mainteiner = $comtec_id_mainteiner ");
                                        while ($r = sqlsrv_fetch_array($responsible,SQLSRV_FETCH_ASSOC))
                                        {
                                            if ($r['status_reg'] == 1)
                                            {
                                             if ($comtec_id_responsible==$r['comtec_id_responsible']){
                                             echo "<option value=".$r['comtec_id_responsible']." selected>".$r['name']." ".$r['lastname1']." ".$r['lastname2']."</option>";
                                             }else {
                                              echo "<option value=".$r['comtec_id_responsible'].">".$r['name']." ".$r['lastname1']." ".$r['lastname2']."</option>";    
                                             }

                                            }
                                        }
                                    ?>
                               </select>
                              </td>
			    </tr>
			    <tr>
				<td width="200">Propietario</td>
				<td>
				    <select id="comtec_id_owner" style="width:257px;">
					<?
					$owners = $db->query("SELECT comtec_id_owner, company_name FROM owner WHERE comtec_id_mainteiner=$comtec_id_mainteiner AND status_reg=1");					
					while ($r = sqlsrv_fetch_array($owners,SQLSRV_FETCH_ASSOC))
					{  
					 if($comtec_id_owner!=$r['comtec_id_owner']){
                                          echo "<option value=".$r['comtec_id_owner'].">".$r['company_name']."</option>";
                                         }else
                                         {
                                          echo "<option value=".$r['comtec_id_owner']." selected>".$r['company_name']."</option>";   
                                         }
					}
					?>
				    </select>
				</td>
				<td width="200">Configuraci&oacute;n</td>
				<td>
				    <select id="comtec_id_configman" style="width:257px;">
					<?
					    $config = $db->query("SELECT comtec_id_configman, configman_name FROM configman WHERE comtec_id_mainteiner=$comtec_id_mainteiner AND status_reg=1 ");					    
					    while ($r = sqlsrv_fetch_array($config,SQLSRV_FETCH_ASSOC))
					    {
					     if($comtec_id_configman!=$r['comtec_id_configman']){   
                                              echo "<option value=".$r['comtec_id_configman'].">".$r['configman_name']."</option>";
					     }else
                                              {
                                               echo "<option value=".$r['comtec_id_configman']." selected>".$r['configman_name']."</option>";   
                                              }                                             
                                            }
					?>
				    </select>
				</td>
				<td>
                                    <!--TODO agregar la description confman-->
				    <div id="descriptionConfman"></div>
				</td>
			    </tr>
			    <tr>
				<td width="200">C&oacute;d. propietario</td>
				<td><input type="text" size="40" maxlength="10" id="cod_owner" value="<?=$cod_owner;?>" /></td>
                                <td width="200">Fecha pr&oacute;x. oper.</td>
                                <td>
                                  <?$optionsXML = simplexml_load_file("../../xml/days.xml"); 
                                  $options =  $optionsXML->xpath("//day/text()");
                                  echo '<select id="maintenance_day" style="width:65px;">';
                                  while(list( $key , $value) = each($options)) {
                                   if ($maintenance_day == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                                   else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                                  }
                                  echo '</select>';?> 

                                  <?$optionsXML = simplexml_load_file("../../xml/months.xml"); 
                                  $options =  $optionsXML->xpath("//month/text()");
                                  echo '<select id="maintenance_month" style="width:105px;">';
                                  while(list( $key , $value) = each($options)) {
                                   if ($maintenance_month == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                                   else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                                  }
                                  echo '</select>';?> 

                                  <?$optionsXML = simplexml_load_file("../../xml/years.xml"); 
                                  $options =  $optionsXML->xpath("//year/text()");
                                  echo '<select id="maintenance_year" style="width:78px;">';
                                  while(list( $key , $value) = each($options)) {
                                   if ($maintenance_year == $value) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
                                   else {echo '<option value='.$value.'>' . $value . '</option>';}
                                  }
                                  echo '</select>';?>
                                </td>
                            </tr>
                            <tr>
				<td width="200">Notas</td>
			    </tr>
			    <tr>
				<td colspan="4"><textarea id="notes" rows="3" cols="118" style="resize:none"><?=$notes;?></textarea></td>
			    </tr>
                            <tr>
                                <td>
                                    <input type="button" name="editMachine" value="Editar" onclick="javascript:editMachine(<?=$comtec_id_machine;?>)" />
                                    <input type="button" value="Cancelar" onclick="javascript:loadMachines();" />
                                </td>
                            </tr>
			</table>			
 <div id="result"></div> 		    
</fieldset>
